//
//  NBALiveViewController.m
//  movement
//
//  Created by lanou on 16/5/11.
//  Copyright © 2016年 lanou. All rights reserved.
//  http://api.avatardata.cn/Nba/NomalRace?key=60bab6fd150745158d7a681fe9534df6

#import "NBALiveViewController.h"
#import "NetworkRequest.h"
#import "LiveModel.h"
#import "LiveTableViewCell.h"
#import "BaseTableViewCell.h"
#import "TableViewCellFactory.h"
#import "LiveDetailsViewController.h"

@interface NBALiveViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *oneArr;
@property (nonatomic, strong) NSMutableArray *twoArr;
@property (nonatomic, strong) NSMutableArray *threeArr;
@property (nonatomic, strong) NSMutableDictionary *dic;
@property (nonatomic, strong) NSMutableArray *headerArray;
@property (nonatomic, assign) NSInteger *aa;

@end

@implementation NBALiveViewController

- (NSMutableArray *)oneArr {
    if (_oneArr == nil) {
        _oneArr = [NSMutableArray array];
    }
    return _oneArr;
}
- (NSMutableArray *)twoArr {
    if (_twoArr == nil) {
        _twoArr = [NSMutableArray array];
    }
    return _twoArr;
}
- (NSMutableArray *)threeArr {
    if (_threeArr == nil) {
        _threeArr = [NSMutableArray array];
    }
    return _threeArr;
}
- (NSMutableDictionary *)dic {
    if (_dic == nil) {
        _dic = [NSMutableDictionary dictionary];
    }
    return _dic;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:(UITableViewStyleGrouped)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerNib:[UINib nibWithNibName:@"LiveTableViewCell" bundle:nil] forCellReuseIdentifier:NSStringFromClass([LiveModel class])];
    [self getData];
}
// 数据解析
- (void)getData {
    [NetworkRequest getUrl:@"http:api.avatardata.cn/Nba/NomalRace?key=60bab6fd150745158d7a681fe9534df6" CallBack:^(id data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSArray *array = dic[@"result"][@"list"];

        NSMutableArray *array0 = [NSMutableArray arrayWithArray:array[0][@"tr"]];
        
        for (NSDictionary *dic in array0) {
            LiveModel *model = [[LiveModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.oneArr addObject:model];
        }
        NSArray *array1 = [NSArray arrayWithArray:array[1][@"tr"]];
        for (NSDictionary *dic in array1) {
            LiveModel *model = [[LiveModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.twoArr addObject:model];
        }
        NSArray *array2 = [NSArray arrayWithArray:array[2][@"tr"]];
        for (NSDictionary *dic in array2) {
            LiveModel *model = [[LiveModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.threeArr addObject:model];
        }
        NSString *a = array[0][@"title"];
        NSString *b = array[1][@"title"];
        NSString *c = array[2][@"title"];
        self.headerArray = @[a, b, c].mutableCopy;
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
        
    }];
}


#pragma mark - UITableViewDelegate
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return self.headerArray[0];
    } else if (section == 1) {
        return self.headerArray[1];
    } else {
        return self.headerArray[2];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return self.oneArr.count;
    } else if (section == 1) {
        return self.twoArr.count;
    } else {
        return self.threeArr.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LiveModel *model = [[LiveModel alloc] init];
    BaseTableViewCell *cell = [TableViewCellFactory cellWithModelClass:[LiveModel class] tableView:tableView index:indexPath];
    if (indexPath.section == 0) {
        model = self.oneArr[indexPath.row];
        [cell setDataWithModel:model];
        return cell;
    } else if (indexPath.section == 1) {
        model = self.twoArr[indexPath.row];
        [cell setDataWithModel:model];
        return cell;
    } else {
        model = self.threeArr[indexPath.row];
        [cell setDataWithModel:model];
        return cell;
    }
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    LiveModel *model = [[LiveModel alloc] init];
    LiveDetailsViewController *liveVC = [[LiveDetailsViewController alloc] init];
    if (indexPath.section == 0) {
        model = self.oneArr[indexPath.row];
    } else if (indexPath.section == 1) {
        model = self.twoArr[indexPath.row];
    } else {
        model = self.threeArr[indexPath.row];
    }
    NSString *a = model.link2url;
    liveVC.url = a;
   
//    liveVC.number = string;
    
    [self.navigationController pushViewController:liveVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
